<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="头像地址" prop="portrait">
      <el-input v-model="dataForm.portrait" placeholder="头像地址"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="regTime">
      <el-input v-model="dataForm.regTime" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="角色" prop="role">
      <el-input v-model="dataForm.role" placeholder="角色"></el-input>
    </el-form-item>
    <el-form-item label="权限表外键" prop="pid">
      <el-input v-model="dataForm.pid" placeholder="权限表外键"></el-input>
    </el-form-item>
    <el-form-item label="最后登录时间" prop="lastLoginTime">
      <el-input v-model="dataForm.lastLoginTime" placeholder="最后登录时间"></el-input>
    </el-form-item>
    <el-form-item label="最后登录IP" prop="lastLoginIp">
      <el-input v-model="dataForm.lastLoginIp" placeholder="最后登录IP"></el-input>
    </el-form-item>
    <el-form-item label="	状态 0 禁用 1 启用" prop="status">
      <el-input v-model="dataForm.status" placeholder="	状态 0 禁用 1 启用"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          phone: '',
          portrait: '',
          userName: '',
          regTime: '',
          role: '',
          pid: '',
          lastLoginTime: '',
          lastLoginIp: '',
          status: ''
        },
        dataRule: {
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          portrait: [
            { required: true, message: '头像地址不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          regTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          role: [
            { required: true, message: '角色不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '权限表外键不能为空', trigger: 'blur' }
          ],
          lastLoginTime: [
            { required: true, message: '最后登录时间不能为空', trigger: 'blur' }
          ],
          lastLoginIp: [
            { required: true, message: '最后登录IP不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '	状态 0 禁用 1 启用不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.phone = data.user.phone
                this.dataForm.portrait = data.user.portrait
                this.dataForm.userName = data.user.userName
                this.dataForm.regTime = data.user.regTime
                this.dataForm.role = data.user.role
                this.dataForm.pid = data.user.pid
                this.dataForm.lastLoginTime = data.user.lastLoginTime
                this.dataForm.lastLoginIp = data.user.lastLoginIp
                this.dataForm.status = data.user.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'phone': this.dataForm.phone,
                'portrait': this.dataForm.portrait,
                'userName': this.dataForm.userName,
                'regTime': this.dataForm.regTime,
                'role': this.dataForm.role,
                'pid': this.dataForm.pid,
                'lastLoginTime': this.dataForm.lastLoginTime,
                'lastLoginIp': this.dataForm.lastLoginIp,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
